﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace GameLib1
{
    /// <summary>
    /// TODO:
    /// * Draw based on Matrix Transformation (GameObject)
    /// * Positions Based in Center
    /// </summary>
    class ObjectManager
    {
        Container<GameObject> mObjects = new Container<GameObject>();

        public static ObjectManager current = new ObjectManager();

        ObjectManager() { }

        public GameObject Add( GameObject obj )
        {
            mObjects.Add(obj);
            return mObjects.Last();
        }

        public GameObject Add( String texture_name )
        {
            mObjects.Add( new GameObject(new Microsoft.Xna.Framework.Vector2(0), texture_name) ) ;

            return mObjects.Last();
        }

        public void Remove(GameObject obj)
        {
            mObjects.Remove(obj);
        }

        public void Clear()
        {
            mObjects.Clear();
        }

        public void Update(float dt)
        {
            foreach (GameObject gameObj in mObjects.mData )
            {
                gameObj.Update(dt);
            }
        }

        public void Draw(SpriteBatch sb)
        {
            foreach (GameObject gameObj in mObjects.mData)
            {
                sb.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, Camera.Transform * gameObj.mTransform);
                if( gameObj.mTexture != null )
                    sb.Draw(gameObj.mTexture, gameObj.mTexture.Bounds, Color.White);
                sb.End(); 
            }
        }

    }
}
